/***********************************************************************
 * POJO Class :  DocD.java
 * Author:  Bill Vison
 * Purpose: Defines the Class SysResource
 ***********************************************************************/

package edu.gtcfla.km.model;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;
import org.vsg.biz.account.User;
import org.vsg.biz.account.model.UserD;
import org.vsg.biz.utils.InjectRepo;

import edu.gtcfla.km.UserGroup;
import edu.gtcfla.km.repositories.UserGroupRepo;

@javax.persistence.Entity
@javax.persistence.Table(name = "USER_GROUP")
public class UserGroupD implements UserGroup, java.io.Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1L;

	@Column(name ="USER_GROUP_ID")
	@GeneratedValue(generator = "paymentableGenerator")      
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid") 	
	@Id	
	private String id;	
	
	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public void setId(String id) {
		// TODO Auto-generated method stub
		this.id = id ;
	}

	@Column(name ="GROUP_NAME")	
	private String groupName;
	
	@Override	
	public String getGroupName() {
		return groupName;
	}

	@Override	
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}
	
	@Column(name ="GROUP_DESC")	
	private String description;

	@Override
	public void setDescription(String newDescription) {
		// TODO Auto-generated method stub
		this.description = newDescription;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return description;
	}

	@javax.persistence.OneToMany(
			cascade = { javax.persistence.CascadeType.ALL}
			,fetch = javax.persistence.FetchType.LAZY )
	@JoinTable(
			name="USER_GROUP_MAP",
			joinColumns = @JoinColumn( name="USER_GROUP_ID"),
			inverseJoinColumns = @JoinColumn( name="USER_ID")
		)	
	private Set<UserD> memberD;

	public Set<UserD> getMemberD() {
		return memberD;
	}

	public void setMemberD(Set<UserD> memberD) {
		this.memberD = memberD;
	}

	@Override
	public void setMembers(Set<User> users) {
		// TODO Auto-generated method stub
		setMemberD( (Set<UserD>)(Set) users  );
	}

	@InjectRepo
	@Transient	
	private UserGroupRepo<UserGroup, String> userGroupRepo;	
	
	public UserGroupRepo<UserGroup, String> getUserGroupRepo() {
		return userGroupRepo;
	}

	public void setUserGroupRepo(UserGroupRepo<UserGroup, String> userGroupRepo) {
		this.userGroupRepo = userGroupRepo;
	}

	@Override
	public Set<User> getMembers() {
		// TODO Auto-generated method stub
		
		Set<User>  members = new LinkedHashSet<User>();

		try {
			
			 members =(Set<User>)(Set) this.getMemberD();
			 
			 if (!members.isEmpty() ) {
				User user =  members.iterator().next();
			 }

		} catch (org.hibernate.LazyInitializationException lie) {
			members = userGroupRepo.findMembersByGroupId( this.getId() );			
		}			

		return members;
	}
	


	



	
	
	
}